package setting

import (
	"github.com/gin-gonic/gin"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/service/setting"
	"likeadmin/core"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/middleware"
	"likeadmin/util"
)

var NoticeGroup = core.Group("/setting/notice", newSettingHandler, regSetting, middleware.TokenAuth())

func newSettingHandler(srv setting.INoticeSettingService) *settingHandler {
	return &settingHandler{srv: srv}
}

func regSetting(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *settingHandler) {
		rg.GET("/list", handle.list)
		rg.GET("/detail", handle.detail)
		rg.POST("/save", handle.edit)

	})
}

type settingHandler struct {
	srv setting.INoticeSettingService
}

// list setting列表
func (hd settingHandler) list(c *gin.Context) {
	var page request.PageReq
	var listReq req.NoticeSettingListReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
		return
	}
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
		return
	}
	res, err := hd.srv.List(c, page, listReq)
	response.CheckAndRespWithData(c, res, err)
}

// detail setting详情
func (hd settingHandler) detail(c *gin.Context) {
	var detailReq req.NoticeSettingDetailReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &detailReq)) {
		return
	}
	res, err := hd.srv.Detail(c, detailReq.Id)
	response.CheckAndRespWithData(c, res, err)
}

// edit setting编辑
func (hd settingHandler) edit(c *gin.Context) {
	var editReq req.NoticeSettingEditReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Edit(c, editReq))
}
